package com.bjjnts.code;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 验证码识别工具
 * 推荐 http://www.kuaishibie.cn/ 注册 推荐码 63a84acc47d248e89dbe2b0d7816e8fa
 */
@Slf4j
public class CaptchaUtil {


    /** http://www.kuaishibie.cn/ 账户 */
    public static final String uname = "xxxx";

    /** http://www.kuaishibie.cn/ 密码 */
    public static final String upass = "xxxx";

    /** 识别 */
    public static JSONObject verifyCode(String imageBase64) throws Exception {
        String url = "http://api.kuaishibie.cn/predict";

        JSONObject params = new JSONObject();
        params.set("username", uname);
        params.set("password", upass);
        params.set("typeid", 7);
        String[] split = imageBase64.split(",");
        params.set("image", split[1]);

        String rep = HttpRequest.post(url)
                .body(JSONUtil.toJsonStr(params)).execute().body();

        log.info(rep);
        if (Objects.equals(JSONUtil.parseObj(rep).getStr("code"), "-1")) {
            throw new Exception("验证码识别失败");
        }

        return JSONUtil.parseObj(rep).getJSONObject("data");
    }

//    /** 复杂 [词典]	complicated*/
//    public static JSONObject verifyCodeComplicated(String imageBase64) {
//        String url = "http://apigateway.jianjiaoshuju.com/api/v_1/fzyzm.html";
//
//        JSONObject params = new JSONObject();
//        params.set("v_pic", imageBase64);
//        params.set("pri_id", "js");
//
//        String rep = HttpRequest.post(url)
//                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
//                .header("appCode", APP_CODE).header("appKey", APP_KEY).header("appSecret", APP_SECRET).form(params).execute().body();
//
//        System.out.println(rep);
//
//        return JSONUtil.parseObj(rep);
//    }
//
//    public static void serviceQr() {
//        String url = "http://apigateway.jianjiaoshuju.com/api/v_1/serviceQ.html";
//
//        String rep = HttpRequest.post(url).header("appCode", APP_CODE).header("appKey", APP_KEY).header("appSecret", APP_SECRET).execute().body();
//        System.out.println(rep);
//    }
}
